import sha256 from 'crypto-js/sha256';
export default{
    data(){
        return {
            
            selection:[],
            id:'',
            editmode:false,
            modal1:false,
            ids:[],
            list:[],
            filter:{
                page:1,
                limit:5,
                name:'',
                title:'',
                total:0,
                cateId:'',
                newsId:'',
                filename:''
            }
        }
    },
    methods:{
        getData(){//加载数据和重载数据
            console.log(this.filter);
                this.$http({
                    method:'post',
                    url:`http://localhost:3000/${this.module}/list`,
                    data:this.filter
                })
                .then(res=>{
                    this.list=res.data.rows;
                     this.filter.total =res.data.total; 
                     //console.log(this.list,'========');
                    // this.list.forEach(function(element) {
                    //     console.log(element._id);
                    //     this.$http({
                    //         method:'post',
                    //         url:`http://localhost:3000/comment/querynewsId`,
                    //         data:{newsId:element._id}
                    //     }).then(res=>{
                    //         console.log(res.data.length);
                    //     })
                    // }, this);
                })
                

        },
        changepage(page){//页码
            this.filter.page=page;
            this.getData();
        },
        onPageSizeChange(pageSize){//每页数量
            this.filter.limit =pageSize;
            console.log(this.filter.limit)
            this.getData();
        },
        removeData(id,index,fsname){//单删
            if(fsname){
                var data={filename:fsname,id:id};
                this.$http.post(`http://localhost:3000/${this.module}/fs`,data).then((response) => {
                    this.getData();
                })

            }else{
                this.$http.delete(`http://localhost:3000/${this.module}/data/`+id).then((response) => {
                    this.getData();
                })
            }
        },
        addData(){//添加
            this.modal1=true;
            this.editmode=false;
            this.handleReset('formValidate');
            this.formValidate.cateId=this.filter.cateId;
            this.formValidate.newsId=this.filter.newsId;
        },
        editData(id){//修改
            this.id=id;
                this.$http.post(`http://localhost:3000/${this.module}/data/`+id).then((response) => {
                    this.formValidate=response.data;
                    this.modal1=true;
                    
                })
        },
        handleReset (name) {//清空
            this.$refs[name].resetFields();
            
        },
        handleSubmit (name){//提交
            // var page=this.filter.page;
            this.formValidate.password=sha256(this.formValidate.password).toString();
            this.$refs[name].validate((valid) => {
                if (valid) {
                    
                    
                    if(this.editmode){
                        this.$http.post(`http://localhost:3000/${this.module}/data/`+this.id,this.formValidate).then((response) => {
                            this.modal1=false;
                            this.$Message.success('提交成功!');
                            this.getData();
                        })
                    }else{
                        delete this.formValidate._id;
                        this.$http.post(`http://localhost:3000/${this.module}/data`,this.formValidate).then((response) => {
                            this.modal1=false;
                            this.$Message.success('提交成功!');
                            this.getData();
                        })
                }
            } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        chose(selection,row){//多选框选中
            this.selection=selection;
        },
        deleteData(){//多项删除
           
            if(this.module=='upload'){
              
                this.selection.forEach(function(element) {
                    var data={
                        id:element._id,
                        filename:element.filename
                    }
                    this.$http.post(`http://localhost:3000/${this.module}/fs`,data).then((response) => {
                   })
               }, this);
            }else{
                
                this.selection.forEach(function(element) {
                    this.$http.delete(`http://localhost:3000/${this.module}/data/`+element._id).then((response) => {
                   })
               }, this);
            }
            this.getData();
        },
        searchitem(){
            console.log(this.filter.filename,this.filter.cateId);
            this.getData();
        }

    },
    created(){
        this.getData();
        
    }

}